package com.cindata

import com.cindata.dictionary.*
import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured


import static org.springframework.http.HttpStatus.*

@Transactional
@Secured(['isAuthenticated()'])
class ContactRiskWarningDispositionController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond ContactRiskWarningDisposition.list(params), model:[contactRiskWarningDispositionCount: ContactRiskWarningDisposition.count()]
    }

    def show(ContactRiskWarningDisposition contactRiskWarningDisposition) {
        respond contactRiskWarningDisposition
    }

    def attachmentShow(ContactRiskWarningDisposition contactRiskWarningDisposition)
    {
        params['targetUri'] = request.getHeader("referer")
        def photoes = Attachment.findAll("from Attachment where contactWarningDisposal.id = ? order by displayOrder, createdDate asc", [contactRiskWarningDisposition?.id])

        respond contactRiskWarningDisposition, model: [photoes: photoes]
    }

    def create() 
    {
        params["targetUri"] = request.getHeader("referer")
        respond new ContactRiskWarningDisposition(params)
    }

    @Transactional
    def save(ContactRiskWarningDisposition contactRiskWarningDisposition) 
    {
        if (contactRiskWarningDisposition == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (contactRiskWarningDisposition.hasErrors()) {
            transactionStatus.setRollbackOnly()
            println contactRiskWarningDisposition.errors
            respond contactRiskWarningDisposition.errors, view:'create'
            return
        }

        if (contactRiskWarningDisposition.validate())
        {
            contactRiskWarningDisposition?.save()
        }
        else
        {
            println contactRiskWarningDisposition?.errors
        }

        redirect url: params["targetUri"]
    }

    def edit(ContactRiskWarningDisposition contactRiskWarningDisposition) 
    {
        params["targetUri"] = request.getHeader("referer")
        respond contactRiskWarningDisposition
    }

    @Transactional
    def update(ContactRiskWarningDisposition contactRiskWarningDisposition) 
    {
        println contactRiskWarningDisposition
        if (contactRiskWarningDisposition == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (contactRiskWarningDisposition.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond contactRiskWarningDisposition.errors, view:'edit'
            return
        }

        contactRiskWarningDisposition.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'contactRiskWarningDisposition.label', default: 'ContactRiskWarningDisposition'), contactRiskWarningDisposition.id])
                redirect url: params["targetUri"]
            }
            '*'{ respond contactRiskWarningDisposition, [status: OK] }
        }
    }

    @Transactional
    def delete(ContactRiskWarningDisposition contactRiskWarningDisposition) 
    {

        if (contactRiskWarningDisposition == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        contactRiskWarningDisposition.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'contactRiskWarningDisposition.label', default: 'ContactRiskWarningDisposition'), contactRiskWarningDisposition.id])
                redirect url: request.getHeader("referer")
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'contactRiskWarningDisposition.label', default: 'ContactRiskWarningDisposition'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }

    def updateRiskWarning()
    {
        def id = params['contactRiskWarningDispositionId']
        def contactRiskWarningDisposition = ContactRiskWarningDisposition.findById(id)
        if (contactRiskWarningDisposition) 
        {
            contactRiskWarningDisposition.disposalOpinions = params['disposalOpinions']
            contactRiskWarningDisposition.disposalDate = new Date()
            contactRiskWarningDisposition.status = "已处置"
            
            if (contactRiskWarningDisposition.validate()) 
            {
                contactRiskWarningDisposition.save flush: true        
            }
            else
            {
                println contactRiskWarningDisposition.errors
            }    
        }
        redirect url: request.getHeader("referer")
    }
}